package work.sajor.wechatpush.service;


import com.alibaba.fastjson.JSONObject;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;


@Service
public class WeatherService {


    public static final Logger logger = LoggerFactory.getLogger(WeatherService.class);

    public JSONObject getHZWeather() {
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        Request request = new Request.Builder()
                .url("https://mk2tunr538.re.qweatherapi.com/v7/weather/now?location=101210101")
                .get()
                .addHeader("X-QW-Api-Key", "41abfae039234fc2a7eaaf9948497d6a")
                .build();
        try {
            Response response = client.newCall(request).execute();
            if (response.isSuccessful()) {
                assert response.body() != null;
                String responseData = response.body().string();
                return JSONObject.parseObject(responseData);
            }
        } catch (Exception e) {
            logger.error("获取杭州天气失败");
        }
        return null;
    }
}
